#!/bin/bash

SSH_OPTIONS="-i /root/documents/id_rsa"
SSH_OPTIONS+=" -o StrictHostKeyChecking=no"
SSH_OPTIONS+=" -o UserKnownHostsFile=/dev/null"
PORT=4920

upload_folder_to_qemu() {
    echo "Uploading \"$1\" folder to QEMU ..."
    scp ${SSH_OPTIONS} -P $PORT -r $1 root@localhost:/root
}

upload_to_qemu() {
    echo "Uploading \"$(basename $1)\" to QEMU ..."
    scp ${SSH_OPTIONS} -P $PORT $1 root@localhost:/root
}

run_in_qemu() {
    echo "Running \"$1\" in QEMU ..."
    ssh ${SSH_OPTIONS} -p $PORT root@localhost "$1"
}

DRIVER_NAME="dmatestdriver"

upload_to_qemu /root/documents/dma-uio-driver/build/linux/x86_64/debug/dma-user
upload_to_qemu /root/documents/dma-uio-driver/build/linux/x86_64/debug/${DRIVER_NAME}.ko


run_in_qemu "modprobe uio"
run_in_qemu "echo 0 > /proc/sys/kernel/randomize_va_space" # 关闭进程地址随机化
run_in_qemu "rmmod ${DRIVER_NAME}.ko"
run_in_qemu "cd /root && insmod ${DRIVER_NAME}.ko"
run_in_qemu "cat /sys/module/${DRIVER_NAME}/sections/.text"

ssh ${SSH_OPTIONS} -p $PORT root@localhost 
